package edge.display.backend.Utils;

import java.util.Map;
import java.util.Queue;

/**
 * 统计工具类
 *
 * @author Amane Hayaashi
 * @date 2020/11/8
 * @since 1.0
 */

public class StatUtils {

    public static double doubleRound2(double d) {
        return (double) (Math.round(d * 100) / 100);
    }

    public static void pushData(Map<String, Queue<Object>> map, String key, Object data) {
        Queue<Object> Queue = map.get(key);
        pushData(Queue, data);
        map.put(key, Queue);
    }

    public static void pushData(Queue<Object> Queue, Object data) {
        Queue.offer(data);
        if (Queue.size() > 15) {
            Queue.poll();
        }
    }

    public static void collectData(Map<String, int[]> map, String key, int index) {
        int[] arr = map.get(key);
        arr[index]++;
        map.put(key, arr);
    }

    public static void averageData(Map<String, Integer> avgMap, Map<String, Integer> valMap, int w) {
        for (String k : avgMap.keySet()) {
            int a = avgMap.get(k);
            int v = valMap.get(k);
            a = (a * (w - 1) + v) / w;
            avgMap.put(k, a);
        }
    }

}
